package com.xique.door.controller;


import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.PermissionGroupDevice;
import com.xique.door.bean.request.permissionGroup.PermissionGroupDeleteDeviceRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupDeviceRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupSelectDeviceRequest;
import com.xique.door.biz.service.IPermissionGroupDeviceBizService;
import com.xique.door.service.IPermissionGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Deprecated
@Controller
@RequestMapping("/oms/permissionGroupDevice")
public class PermissionGroupDeviceController extends BaseController {

    private String prefix = "oms/permissionGroupDevice";

    @Autowired
    private IPermissionGroupService permissionGroupService;

    @Autowired
    private IPermissionGroupDeviceBizService permissionGroupDeviceBizService;

    /**
     * 分配设备
     */
    @PreAuthorize(hasPermi = "oms:permissionGroup:edit")
    @GetMapping("/addDevice/{id}")
    public String addDevice(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("group", permissionGroupService.selectPermissionGroupById(id));
        return prefix + "/addDevice";
    }

    /**
     * 查询已分配设备权限组列表
     */
    @PreAuthorize(hasPermi = "oms:permissionGroup:list")
    @PostMapping("/addDevice/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(PermissionGroupDeviceRequest request) {
        startPage();
        List<PermissionGroupDevice> list = permissionGroupDeviceBizService.selectAllocatedList(request);
        return getDataTable(list);
    }

    /**
     * 查询未分配设备权限组列表
     */
    @PreAuthorize(hasPermi = "oms:permissionGroup:list")
    @PostMapping("/addDevice/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(PermissionGroupDeviceRequest request) {
        startPage();
        List<PermissionGroupDevice> list = permissionGroupDeviceBizService.selectUnallocatedList(request);

        TableDataInfo tableDataInfo = getDataTable(list);

        List<PermissionGroupDevice> lists = new ArrayList<>(list.size());
        list.forEach(item -> {
            item.setGroupId(request.getGroupId());
            lists.add(item);
        });

        tableDataInfo.setRows(lists);

        return tableDataInfo;
    }

    /**
     * 新增权限组设备
     */
    @GetMapping("/addDevice/selectDevice/{id}")
    public String selectDevice(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("group", permissionGroupService.selectPermissionGroupById(id));
        return prefix + "/selectDevice";
    }

    /**
     * 批量选择设备授权
     */
    @Log(title = "权限管理", businessType = BusinessType.GRANT)
    @PostMapping("/addDevice/selectAll")
    @ResponseBody
    public AjaxResult selectDeviceAll(PermissionGroupSelectDeviceRequest request) {
        return permissionGroupDeviceBizService.addDevices(request);
//        return permissionGroupService.selectDevices(request);
    }

    /**
     * 取消授权
     */
    @Log(title = "权限管理", businessType = BusinessType.GRANT)
    @PostMapping("/addDevice/cancel")
    @ResponseBody
    public AjaxResult cancelDevice(PermissionGroupDeleteDeviceRequest request) {
        return permissionGroupDeviceBizService.deleteDevice(request);
//        return toAjax(permissionGroupService.deleteDevice(request));
    }

    /**
     * 批量取消授权
     */
    @Log(title = "权限管理", businessType = BusinessType.GRANT)
    @PostMapping("/addDevice/cancelAll")
    @ResponseBody
    public AjaxResult cancelDeviceAll(PermissionGroupDeleteDeviceRequest request) {
        return permissionGroupDeviceBizService.deleteDevices(request);
//        return toAjax(permissionGroupService.deleteDevices(request));
    }
}
